﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using ConfigLibrary;
using System.IO;
using MySql.Data.MySqlClient;
using MySql.Data;

namespace rfins
{
    public class mysqlconn
    {
        public mysqlconn()
        {
            try
            {
                Stream s;
            #if (DEBUG)
                { s = File.Open(Properties.Settings.Default.CONFIGTEST, FileMode.Open); }

            #elif! (DEBUG)            
            {s = File.Open(Properties.Settings.Default.CONFIGDEV, FileMode.Open);}
            #endif

                BinaryFormatter b = new BinaryFormatter();
                config = (Config)b.Deserialize(s);
                s.Close();
                config.GetSqlInfo(out _host, out _username, out _pass, out _database, out _port);
            }
            catch (Exception ex)
            {
                errorhandling.ErrorEmail("config file not working", ex);
            }
        }

        public void connectwmdata()
        {
            //this establishes the mysql connection string
            
            try
            {
                database = _database.Split(new Char[] { ' ', ',' });
                conn.ConnectionString = "Database=" + database[1] + ";Data Source=" + _host + ";Port=" + _port + ";User Id=" + _username + ";Password=" + _pass;                
            }
            catch (Exception ex)
            {
                errorhandling.ErrorEmail("Connection string error", ex);
            }
        }

        public MySqlConnection conn = new MySqlConnection();
        Config config;
        public string[] database;
        string _host;
        string _username;
        string _pass;
        string _database;
        int _port;
        
    }
}
